package cms.api.solr.index.controller;

import cms.api.framework.search.IndexService;
import cms.api.framework.search.Indexable;
import cms.api.framework.search.ListQueryParams;
import cms.api.framework.search.PageQueryParams;
import cms.api.framework.search.QueryParams;
import cms.api.solr.index.model.SolrDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Solr索引服务控制器
 *
 * @author heyingcheng
 * @email heyingcheng@ctvit.com.cn
 * @date 2019/6/30 8:02
 */
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private IndexService indexService;

    @PostMapping
    public Object createIndex(@RequestParam String index) {
       return indexService.createIndex(index);
    }

    @DeleteMapping
    public Object deleteIndex(@RequestParam String index) {
        return indexService.deleteIndex(index);
    }

    @GetMapping
    public Object isIndexExist(@RequestParam String index) {
        return indexService.isIndexExist(index);
    }

    @PostMapping("document")
    public Object addDocument(@RequestParam String index, @RequestBody SolrDocument solrDocument) {
        return indexService.addDocument(index, null, solrDocument);
    }

    @PostMapping("documents")
    public Object addDocuments(@RequestParam String index, @RequestBody List<SolrDocument> solrDocuments) {
        List<Indexable> documents = new ArrayList<>();
        documents.addAll(solrDocuments);
        return indexService.addDocuments(index, null, documents);
    }

    @DeleteMapping("document")
    public Object deleteDocumentById(@RequestParam String index, @RequestParam String id) {
        return indexService.deleteDocumentById(index, null, id);
    }

    @DeleteMapping("documents/string")
    public Object deleteDocuments(@RequestParam String index, @RequestParam String ids) {
        return indexService.deleteDocumentByIds(index, null, ids);
    }

    @DeleteMapping("documents/list")
    public Object deleteDocuments(@RequestParam String index, @RequestParam("ids") List<String> ids) {
        return indexService.deleteDocumentByIds(index, null, ids);
    }

    @DeleteMapping("document/query")
    public Object deleteDocumentByQuery(@RequestBody QueryParams queryParams) {
        return indexService.deleteDocumentByQuery(queryParams.getIndex(), null, queryParams);
    }

    @PutMapping("document")
    public Object updateDocument(@RequestParam String index, @RequestParam Boolean isUpsert, @RequestBody SolrDocument solrDocument) {
        return indexService.updateDocument(index, null, solrDocument, isUpsert);
    }

    @PutMapping("documents")
    public Object updateDocuments(@RequestParam String index, @RequestBody List<SolrDocument> solrDocuments) {
        List<Indexable> documents = new ArrayList<>();
        documents.addAll(solrDocuments);
        return indexService.updateDocuments(index, null, documents, true);
    }

    @GetMapping("document/fetch/id")
    public Object fetchDocumentById(@RequestParam String index, @RequestParam String id, @RequestParam(value = "rtnField", required = false) String[] rtnFields) {
        return indexService.fetchDocumentById(index, null, id, rtnFields);
    }

    @GetMapping("document/fetch/string")
    public Object fetchDocumentByIds(@RequestParam String index, @RequestParam String ids, @RequestParam(value = "rtnField", required = false) String[] rtnFields) {
        return indexService.fetchDocumentByIds(index, null, ids, rtnFields);
    }

    @GetMapping("document/fetch/list")
    public Object fetchDocumentByIds(@RequestParam String index, @RequestParam("ids") List<String> ids, @RequestParam(value = "rtnField", required = false) String[] rtnFields) {
        return indexService.fetchDocumentByIds(index, null, ids, rtnFields);
    }

    @GetMapping("document/query/page")
    public Object search(@RequestBody PageQueryParams queryParams) {
        return indexService.search(queryParams);
    }

    @GetMapping("document/query/list")
    public Object search(@RequestBody ListQueryParams queryParams) {
        return indexService.search(queryParams);
    }

    @GetMapping("document/query/string")
    public Object search(@RequestParam String index, @RequestParam String queryString) {
        return indexService.search(index, null, queryString);
    }

    @PostMapping("mapping")
    public Object setMapping(String index, String type, String mapping) {
        indexService.setMapping(index, null, mapping);
        return true;
    }

}
